﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.Sql;
using PTExpress.DataAccess;

namespace PTExpress.Views
{
    /// <summary>
    /// Interaction logic for frmChonDatabase.xaml
    /// </summary>
    public partial class frmChonDatabase : Window
    {
        bool Reload = true;
        public string ConnectionString = string.Empty;
        public frmChonDatabase()
        {
            InitializeComponent();
        }

        //Button click
        private void btnFindServer_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataTable tb = new DataTable();
                tb = SqlDataSourceEnumerator.Instance.GetDataSources();
                DataTable _tb = new DataTable();
                _tb.Columns.Add("InstanceName");
                foreach (DataRow row in tb.Rows)
                {
                    _tb.Rows.Add(row["ServerName"].ToString() + "\\" + row["InstanceName"].ToString());
                }
                cbbServerName.ItemsSource = _tb.DefaultView;
                if (cbbServerName.ItemsSource != null)
                    cbbServerName.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //Load danh sach database
        private void cbbDatabase_GotFocus(object sender, RoutedEventArgs e)
        {
            if (Reload)
            {
                DataTable tb = DataProvider.GetAllDatabase(cbbServerName.Text, (bool)rbtSqlAuthentication.IsChecked, txtDatabase.Text, txtUser.Text, txtPass.Password);
                ((ComboBox)sender).ItemsSource = tb.DefaultView;
            }
            Reload = false;

        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog ofDialog = new System.Windows.Forms.OpenFileDialog();
            ofDialog.InitialDirectory = "c:\\";
            ofDialog.Filter = "Database mdf (*.mdf)|*.mdf";
            ofDialog.RestoreDirectory = true;

            if (ofDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    if (ofDialog.OpenFile() != null)
                    {
                        txtDatabase.Text = ofDialog.FileName;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            switch (tabMain.SelectedIndex)
            {
                case 0:
                    ConnectionString = DataProvider.CreateSqlConnection(cbbServerName.Text, (bool)rbtSqlAuthentication.IsChecked, cbbDatabase.Text, txtUser.Text, txtPass.Password);
                    break;
                case 1:
                    ConnectionString = DataProvider.CreateAttachConnection(cbbServerName.Text, (bool)rbtSqlAuthentication.IsChecked, txtDatabase.Text, txtUser.Text, txtPass.Password);
                    break;
            }
            this.Close();
        }

        private void cbbServerName_GotFocus(object sender, RoutedEventArgs e)
        {
            Reload = true;
        }
    }
}
